﻿ using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using MODEL;

namespace DAL
{
    public class DBContextFactory : IDBContextFactory
    {
        #region  创建 EF上下文 对象，在线程中共享 一个 上下文对象  +DbContext GetDbContext()
        public DbContext GetDbContext(string type)
        {
            DbContext DBContext = CallContext.GetData(type) as DbContext;
            if (DBContext == null)
            {
                switch (type)
                {
          
                case "AmazonCategoryInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AmazonEnum":DBContext = new AmazonSystemEntities();break;
          
                case "AmazonItemOperateion":DBContext = new AmazonSystemEntities();break;
          
                case "AmperageDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ApertureDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoAccessoryInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoAccessoryMiscColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoAccessoryMiscInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoAccessoryProductTypeInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoPartColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoPartInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoPartPartInterchangeDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "AutoPartVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BagCaseInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BatteryInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BatteryPowerIntegerDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BatterySubgroupInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BeautyInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BeautyMiscInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BeautyMiscUnitCountInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BeautyMiscVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BeautyProductTypeInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BinocularInfo":DBContext = new AmazonSystemEntities();break;
          
                case "BlankMediaInfo":DBContext = new AmazonSystemEntities();break;
          
                case "CamcorderInfo":DBContext = new AmazonSystemEntities();break;
          
                case "CameraPhotoInfo":DBContext = new AmazonSystemEntities();break;
          
                case "CameraPhotoProductTypeInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ClothingAccessoriesClassificationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ClothingAccessoriesInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ClothingAccessoriesVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ClothingSizeDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ComputerPlatformInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ConditionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "DegreeDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "DimensionsInfo":DBContext = new AmazonSystemEntities();break;
          
                case "HelmetColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "HelmetInfo":DBContext = new AmazonSystemEntities();break;
          
                case "HelmetVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "LengthDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "MemorySizeDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "MSRPInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PixelDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsPartColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsPartInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsPartPartInterchangeDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsPartVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsVehicleColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsVehicleInfo":DBContext = new AmazonSystemEntities();break;
          
                case "PowersportsVehicleVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProductDescriptionDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProductDiscoveryDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProductInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProductProductDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProductPromoTagInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProtectiveGearColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProtectiveGearInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ProtectiveGearVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "RebateInfo":DBContext = new AmazonSystemEntities();break;
          
                case "RelatedProductIDInfo":DBContext = new AmazonSystemEntities();break;
          
                case "RidingApparelColorSpecificationInfo":DBContext = new AmazonSystemEntities();break;
          
                case "RidingApparelInfo":DBContext = new AmazonSystemEntities();break;
          
                case "RidingApparelVariationDataInfo":DBContext = new AmazonSystemEntities();break;
          
                case "StandardProductIDInfo":DBContext = new AmazonSystemEntities();break;
          
                case "StringLengthOptionalDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "SunProtectionDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "VoltageDecimalDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "VolumeDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "WattageDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "WeightDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "WeightIntegerDimensionInfo":DBContext = new AmazonSystemEntities();break;
          
                case "ZoomDimensionInfo":DBContext = new AmazonSystemEntities();break;
default : DBContext = new AmazonSystemEntities(); break;
                }
                DBContext.Configuration.ValidateOnSaveEnabled = false;
                CallContext.SetData(type, DBContext);
            }
            return DBContext;
        } 
        #endregion
    }

    public class DBSessionFactory:IDBSessionFactory
    {
        #region 在线程中 共用一个 DBSession 对象
        /// <summary>
        /// 在线程中 共用一个 DBSession 对象
        /// </summary>
        /// <returns></returns>
        public IDBSession GetDBSession()
        {
            //从当前线程中  获得DBContext    数据仓储对象
            IDBSession DBSession = CallContext.GetData(typeof(DBSessionFactory).Name) as DBSession;
            if (DBSession==null)
            {
                DBSession = new DBSession();
                CallContext.SetData(typeof(DBSessionFactory).Name, DBSession);
            }
            return DBSession;
        }
        #endregion
    }
}